﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;

namespace V2TEK.NET.SH.Silverlight.LogManager
{
    public class LogCache
    {
        public LogCache()
        {
        }

        public LogClass LogClass
        {
            get;
            set;
        }

        private int mSingleSizeM = 1;
        /// <summary>
        /// 单文件尺寸
        /// </summary>
        public int SingleSizeM
        {
            get
            {
                return mSingleSizeM;
            }
            set
            {
                mSingleSizeM = value;
                mMaxLength = mSingleSizeM * 1024 * 1024;
            }
        }
        private int mMaxLength = 1024 * 1024;

        private StringBuilder _sb = new StringBuilder();
        private System.Collections.Generic.List<StringBuilder> _cache = new System.Collections.Generic.List<StringBuilder>();

        public System.Collections.Generic.List<StringBuilder> Cache
        {
            get
            {
                return _cache;
            }
        }

        public StringBuilder Current
        {
            get
            {
                return _sb;
            }
        }

        protected void Append(string msg)
        {
            _sb.AppendLine(msg);
            if (_sb.Length > mMaxLength)
            {
                _cache.Add(_sb);
                _sb = new StringBuilder();
            }
        }

        public void Error(string msg)
        {
            msg = DateTime.Now.ToString() + "\tError:\t" + msg;
            Append(msg);
        }
        public void Info(string msg)
        {
            if (LogClass >= LogClass.Info)
                return;
            msg = DateTime.Now.ToString() + "\tInfo:\t" + msg;
            Append(msg);
        }
        public void Warning(string msg)
        {
            if (LogClass >= LogClass.Warning)
                return;
            msg = DateTime.Now.ToString() + "\tWarning:\t" + msg;
            Append(msg);
        }

    }

    public enum LogClass
    {
        Info = 2,
        Warning = 1,
        Error = 0,
    }
}
